import jsonpath

from tools.handle_data import Data


class HandleExtract:


    def handle_extract(self,extract_data,response):

        for key,value in extract_data.items():
            extract_data_res=jsonpath.jsonpath(response,value)

            if extract_data_res:
                setattr(Data,key,extract_data_res[0])
                print(getattr(Data, key))

if __name__ == '__main__':
    extract_data={"code":"$.code","msg":"$.msg"}
    response={'code': 0, 'msg': 'OK', 'data': {'id': 52, 'leave_amount': 2500.0, 'mobile_phone': '18820992515', 'reg_name': '小柠檬', 'reg_time': '2022-08-31 16:01:01.0', 'type': 1, 'token_info': {'token_type': 'Bearer', 'expires_in': '2022-09-21 11:14:30', 'token': 'eyJhbGciOiJIUzUxMiJ9.eyJtZW1iZXJfaWQiOjUyLCJleHAiOjE2NjM3MzAwNzB9.7xkLhcskQ6PPBQkPrr5iiNYgsTlzoluw7a5rzKfmghqCr6X59O7v6cq1KoIiPqRYUcYW4LcDEagQK9TrERG3XQ'}}, 'copyright': 'Copyright 柠檬班 © 2017-2019 湖南省零檬信息技术有限公司 All Rights Reserved'}
    HandleExtract().handle_extract(extract_data,response)
    print(getattr(Data,"code"),getattr(Data,"msg"))